<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 工序数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\StepService;
use common\validate\Step;
use support\ErrorCode;
use think\Request;

class StepController extends VerifyController
{
    protected $stepService;

    public function __construct()
    {
        parent::__construct();
        $this->stepService = StepService::getInstance();
    }

    /**
     * 查询工序列表 包含关联的分类 父级信息
     * @param Request $request
     * @return \think\Response
     */
    public function selectWithParentCate(Request $request)
    {
        $res = $this->stepService->selectWithParentCate($request->param());
        return json(success_response('', $res));
    }

    /**
     * 获得一颗工序树 with 审批数据
     * @param Request $request
     * @return \think\Response
     */
    public function selectReview(Request $request)
    {
        $res = $this->stepService->selectReview();
        return json(success_response('', $res));
    }

    /**
     * 绑定审核流
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function bindReviewWorkflow(Request $request)
    {
        $dataList = $request->param()['data_list'];
        $validate = new Step();
        $validate->scene('BindReviewWorkflowItem');
        foreach ($dataList as $data) {
            if (!$validate->check($data)) {
                throw_strack_exception('data_list ' . $validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
            if (!array_key_exists('attr_str', $data)) {
                throw_strack_exception('data_list attr_str require', ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        $res = $this->stepService->batchBindReviewWorkflow($dataList);
        return json(success_response('', $res));
    }

    /**
     * 工序分类=>工序  树
     * @param Request $request
     * @return \think\Response
     */
    public function selectCateTree(Request $request)
    {
        $res = $this->stepService->selectStepsWithCate();
        return json(success_response('ok', $res));
    }

    /**
     * 前置工序-工序详情
     * @param Request $request
     * @return \think\Response
     */
    public function projectTemplateStepList(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->stepService->projectTemplateStepList($param, $param['project_template_id']);
        return json(success_response('ok', $res));
    }

    /**
     * 获取上游
     * @param \think\Request $request
     * @return \think\Response
     */
    public function getUpstreamStepCategory(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->stepService->getUpstreamStepCategory($param['filter']['project_id'], $param['filter']['step_id']);
        return json(success_response('ok', $res));

    }
}
